/*
 * 主罗拉编辑
 * 舞者无罪
 * 2014.12.04
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "rude.h"
#include "com.h"
#include "keypane.h"
#include "mh/common.h"
#include "info.h"

#define RAB_CAPTION_LEFT     (CX_EDGE_LEFT)
#define RAB_CAPTION_TOP      (CY_EDGE_TOP + CX_EDGE_LEFT)
#define RAB_CAPTION_WIDTH    (40)
#define RAB_CAPTION_HEIGHT   (26)
#define RAB_CAPTION_INTER    (CX_RAB_BMP - CX_EDGE_LEFT * 2) / 4
/*
 *
 */
static void CreateRudeInterface(HWND hWnd)
{
    char str[32] = {0};
    HWND hTmp = -1;
    int i = 0, j = 0;
    uint used_sect = GetUsedRabSect(g_knit_data);
    CreateWindow (MH_STATIC_NAME,
    "",
    WS_CHILD | WS_VISIBLE,
    ID_ESC,
    CX_RAB_BMP - CX_ASK_BMP-20,
    10,
    CX_ASK_BMP,
    CY_ASK_BMP,
    hWnd, (int)&g_bmp[BMP_FALSE]);

    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            int item = j * 4 + i + 1;
            sprintf(str, "[%02d]", item);
            hTmp = CreateWindow(MH_BTN_NAME,
                str,
                WS_CHILD | WS_VISIBLE | BS_FLAT,
                ID_BUTTON1 + j * 4 + i,
                13 + i * 118,
                53 + j * 26,
                40,
                26,
                hWnd,
                MH_BTN_BKGND_BLACK | ((used_sect & (1 << (item - 1))) ? MH_BTN_TEXT_RED : MH_BTN_TEXT_GREEN));
            EnableWindow(hTmp, FALSE);
            sprintf(str, "%d", GetRudeDumu(g_knit_data, j * 4 + i));
            CreateWindow(MH_EDIT_NAME,
                str,
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT1 + j * 4 + i,
                63 + i * 118,
                53 + j * 26,
                40,
                26,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    short n = atoi(sc);
    SetRudeDumu(g_knit_data, nID - IDC_EDIT1, n);
    data.rab.change = TRUE;
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 * copy edits from setting just now
 */
static void CopyEdit(HWND hWnd)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        const char *sc = GetWindowCaption(hTmp);
        short n = atoi(sc);
        CopyRudeDumu(g_knit_data, n);
        int i = 0;
        for(i = 0; i < 32; ++i)
        {
            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + i);
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
}
static void EditValue(HWND hWnd, ushort nID)
{
    HWND hTmp = GetDlgItem(hWnd, nID);
    RECT rect;
    GetClientRect(hTmp, &rect);
    ClientToScreen(hTmp, &rect.left, &rect.top);
    ClientToScreen(hTmp, &rect.right, &rect.bottom);
    CreateKeyboardWindow(hWnd, rect, -128, 127, atoi(GetWindowCaption(hTmp)));
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 100, atoi(GetWindowCaption(hTmp)));
    }
    else if(nID == ID_ESC)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.rab.change)
        {
            char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR) + sizeof(".spd")] = { 0 };
            strcat(sc, KNIT_FILE_DIR);
            strcat(sc, GetKnitFileName(g_knit_data));
            strcat(sc, ".spd");
            int ret = WriteSpeedFile(sc, g_knit_data);
            if(ret)
            {
                AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
            }
            SendSpeedParam(g_knit_data);
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
#ifdef GUI_KEYBOARD_MODE
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            EditValue(hWnd, nID);
        }
#else
        OnBtnCmd(hWnd, nID);
#endif
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if(((nID - IDC_EDIT1) & 3) < 3)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if((nID - IDC_EDIT1) & 3)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 4 && nID < IDC_EDIT1 + 32)
        {
            nID -= 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 28)
        {
            nID += 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_C)
    {
        CopyEdit(hWnd);
    }
}
/*
 *
 */
static int RudeWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRudeInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_RAB]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRudeWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_RAB_BMP) / 2;
    int y = (CY_SCREEN - CY_RAB_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(702);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RudeWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_RAB_BMP;
    CreateInfo.by = y + CY_RAB_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
